﻿@using CodeGenerator.Common
@using CodeGenerator.Core
@using System.Text
@{
    MybatisCodeCreate model = Model;
    string entity = model.EntityPackage + "." + Utils.Pascal(model.Name);
    string columnStr = "";
    string whereStr = "";

    for (int i = 1; i <= model.TableInfo.ColumnList.Count; i++)
    {
        ColumnInfo column = model.TableInfo.ColumnList[i - 1];
        if (i != model.TableInfo.ColumnList.Count)
        {
            columnStr += column.Name.ToLower() + ",";
        }
        else
        {
            columnStr += column.Name.ToLower();
        }
    }

    for (int i = 1; i <= model.TableInfo.PrimaryKeyColumns.Count; i++)
    {
        ColumnInfo column = model.TableInfo.PrimaryKeyColumns[i - 1];

        if (i != model.TableInfo.PrimaryKeyColumns.Count)
        {
            whereStr += column.Name.ToLower() + " = #{" + column.Name.ToLower() + ",jdbcType=" + column.JavaType.JdbcType + "}" + " and ";
        }
        else
        {
            whereStr += column.Name.ToLower() + "= #{" + column.Name.ToLower() + ",jdbcType=" + column.JavaType.JdbcType + "}";
        }
    }

    if (!string.IsNullOrEmpty(whereStr))
    {
        whereStr = "where " + whereStr;
    }

}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE MAPPER PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
@{
    @:<mapper namespace="@(model.DaoPackage).@Pascal(model.Name)Dao">
        @:<resultmap id="BaseResultMap" type="@entity">
            foreach (ColumnInfo column in model.TableInfo.ColumnList)
            {
            @:<id column="@column.Name.ToLower()" jdbctype="@column.JavaType.JdbcType" property="@column.Name.ToLower()" />
            }
        @:</resultmap>
        @:<sql id="Base_Column_List">
            @columnStr
        @:</sql>
        @:<select id="selectByPrimaryKey" parametertype="java.lang.String" resultmap="BaseResultMap">
            @:select
            @:<include refid="Base_Column_List" />
            @:from @(model.TableInfo.Name)
            @whereStr
        @:</select>
        @:<delete id="deleteByPrimaryKey" parametertype="java.lang.String">
            @:delete from @(model.TableInfo.Name)
            @whereStr
        @:</delete>
        @:<insert id="insert" parametertype="@entity">
            {
                string insertKey = "";
                string insertValue = "";
                for (int i = 1; i <= model.TableInfo.ColumnList.Count; i++)
                {
                    ColumnInfo column = model.TableInfo.ColumnList[i - 1];
                    if (i != model.TableInfo.ColumnList.Count)
                    {
                        insertKey += column.Name.ToLower() + ",";
                        insertValue += "#{" + column.Name.ToLower() + ",jdbcType=" + column.JavaType.JdbcType + "}" + ",";
                    }
                    else
                    {
                        insertKey += column.Name.ToLower();
                        insertValue += "#{" + column.Name.ToLower() + ",jdbcType=" + column.JavaType.JdbcType + "}";
                    }
                }

                @:insert into @(model.TableInfo.Name) (@insertKey)
                @:values (@insertValue)
            }
        @:</insert>
        @:<insert id="insertSelective" parametertype="@entity">
            @:insert into @(model.TableInfo.Name)
            @:<trim prefix="(" suffix=")" suffixoverrides=",">
                foreach (ColumnInfo column in model.TableInfo.ColumnList)
                {
                @:<if test="@column.Name != null">
                    @:@(column.Name),
                @:</if>
                }
            @:</trim>
            @:<trim prefix="values (" suffix=")" suffixoverrides=",">
                foreach (ColumnInfo column in model.TableInfo.ColumnList)
                {
                @:<if test="@column.Name != null">
                    @:#{@(column.Name),jdbcType=@(column.JavaType.JdbcType)},
                @:</if>
                }
            @:</trim>
            @:</insert>
        @:<update id="updateByPrimaryKeySelective" parametertype="@entity">
            @: update @(model.TableInfo.Name)
            @: <set>
                foreach (ColumnInfo column in model.TableInfo.ColumnList)
                {
                @:<if test="@column.Name != null">
                    @:@(column.Name) = #{@(column.Name),jdbcType=@(column.JavaType.JdbcType)},
                @:</if>
                }
            @:</set>
            @whereStr
        @:</update>
        @:<update id="updateByPrimaryKey" parametertype="@entity">
            @:update @(model.TableInfo.Name)
            @:set
            foreach (ColumnInfo column in model.TableInfo.ColumnList)
            {
                @:@(column.Name) = #{@(column.Name),jdbcType=@(column.JavaType.JdbcType)},
            }

            @(model.TableInfo.Name)
        @:</update>
        @:<select id="selectList" parametertype="@entity" resulttype="@entity">
            @: select * from @(model.TableInfo.Name) where 1=1
            foreach (ColumnInfo column in model.TableInfo.ColumnList)
            {
            @:<if test="@column.Name != null">
                @:and @(column.Name) = #{@(column.Name),jdbcType=@(column.JavaType.JdbcType)}
            @:</if>
            }
        @:</select>
}
</mapper>